package com.villaday.idf.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.villaday.idf.R;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.OnAdapterCallbackListener;
import com.villaday.idf.custom.MyGridView;
import com.villaday.idf.model.PublicEntity;
import com.villaday.idf.model.VipsMemberOrders;
import com.villaday.idf.model.vipsGroupsEntity;
import com.villaday.idf.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页动态列表
 */

public class CommissionSettingAdapter extends AbsRecyclerViewAdapter<vipsGroupsEntity.BodyBean> {
    private OnAdapterCallbackListener onAdapterCallbackListener;
    private View.OnClickListener onClickListener;
    private Context mContext;
    private List<PublicEntity> dataList = new ArrayList<>();
    private GroupManageAdapter groupManageAdapter;
    private boolean isExpand;
    vipsGroupsEntity.BodyBean bean;

    public CommissionSettingAdapter(Context context, OnAdapterCallbackListener onAdapterCallbackListener) {
        super(context, R.layout.recyclerview_commission_setting, R.layout.item_page_loading,
                R.layout.item_page_bottom);
        this.onAdapterCallbackListener = onAdapterCallbackListener;
        this.mContext = context;

        for (int i = 0; i < 20; i++) {
            dataList.add(new PublicEntity(null, "安华西湖" + i));

        }
    }

   /* @Override
    public int getItemType(vipsGroupsEntity.BodyBean d) {
        return d;
    }*/

    @Override
    public void onBindHolder(AbsRecyclerViewAdapter.RecyclerViewHolder holder, final vipsGroupsEntity.BodyBean d, final int position) {
/*        switch (d) {
            case 0: {*/
        holder.bindTextView(R.id.tv_group_name, d.getName() + "(" + d.getQuantity() + "人)")
                .bindTextView(R.id.tv_level2, "分佣比例" + d.getBrokenPercent() + "%");
        MyGridView gridView = (MyGridView) holder.getView(R.id.grid_member2);
        groupManageAdapter = new GroupManageAdapter(mContext);
        gridView.setAdapter(groupManageAdapter);


        if (d.getMembers().size() >= 5) {
            groupManageAdapter.setNumber(d.getMembers().get(0).isExpand());
            holder.setViewVisible(R.id.llayout_look_more, View.VISIBLE);
            if (d.getMembers().get(0).isExpand()) {
                holder.bindTextView(R.id.tv_expand_all, "收起全部")
                        .setViewSelected(R.id.tv_expand_all, true);
            } else {
                holder.bindTextView(R.id.tv_expand_all, "展开全部")
                        .setViewSelected(R.id.tv_expand_all, false);
            }
        } else {
            if (d.getMembers().size() > 0)
                groupManageAdapter.setNumber(!d.getMembers().get(0).isExpand());
            holder.setViewVisible(R.id.llayout_look_more, View.GONE);
        }
        final List<vipsGroupsEntity.BodyBean.MembersBean> memberList = new ArrayList<>();
        memberList.clear();
        memberList.addAll(d.getMembers());
        memberList.add(new vipsGroupsEntity.BodyBean.MembersBean());
        groupManageAdapter.replace(memberList);
        holder.setOnClickListener(R.id.tv_expand_all)
                .setOnClickListener(R.id.tv_level2);

        TextView tv_level2 = (TextView) holder.getView(R.id.tv_level2);
        tv_level2.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (mlistener != null) {
                    mlistener.onLongClick(position);
                }
                return false;
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int childPositon, long id) {
                if (childPositon <= d.getMembers().size() - 1) {
//                    ToastUtil.initToast(mContext, d.getMembers().get(position).getNickName());
                    if (mlistener != null) {
                        mlistener.onChildrenHead(position, childPositon);
                    }
                } else {
                    if (mlistener != null) {
                        mlistener.onAddMember(position);
                    }
                }
            }
        });
/*            }
            break;
            case 1://滑到底，加载下一页
                onAdapterCallbackListener.onCallback();
                break;
        }*/
    }


//    public void setExpand(int position) {
////        dataList.get(position).setExpand(!dataList.get(position).isExpand());
//        bean.getMembers().get(position).setExpand(!bean.getMembers().get(position).isExpand());
//    }


    public interface OnLongListener {
        public void onLongClick(int position);

        public void onChildrenHead(int poition, int childPostion);

        public void onAddMember(int poition);
    }

    public OnLongListener mlistener;

    public void setOnLongListener(OnLongListener listener) {
        this.mlistener = listener;
    }


}
